package com.item.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.item.mall.entity.Cart;
import com.item.mall.entity.Product;
import com.item.mall.mapper.CartMapper;
import com.item.mall.mapper.ProductMapper;
import com.item.mall.service.CartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.item.mall.vo.CartVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张津瑞、彭家博
 * @since 2021-07-31
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Autowired
    CartMapper cartMapper;
    @Autowired
    ProductMapper productMapper;

    @Override
    public List<CartVO> getCartList(Integer userId) {
        return cartMapper.cartVOList(userId);
    }

    @Override
    public boolean addQuantity(Integer productId, Integer quantity, Float cost, Integer userId) {
        Cart cart =new Cart();
        cart.setQuantity(quantity);
        cart.setProductId(productId);
        cart.setUserId(userId);
        cart.setCost(cost);
        QueryWrapper queryWrapper =new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("product_id",productId);
        if(cartMapper.update(cart,queryWrapper)==1) return true;
        return false;
    }

}
